package com.dmcb.trade.business.crawlers.author;

import com.dmcb.common.business.constants.CommonConstant;
import com.dmcb.common.business.utils.JsonUtil;
import com.dmcb.common.business.utils.ListUtil;
import com.dmcb.common.business.utils.WebUtil;
import com.dmcb.common.web.conversion.JsonResult;
import com.dmcb.trade.business.constants.CrawlConstant;
import com.dmcb.trade.business.crawlers.article.BaiJiaHaoParser;
import com.dmcb.trade.business.entities.Article;
import com.dmcb.trade.business.entities.Author;
import com.dmcb.trade.business.mappers.ArticleCrawlMapper;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by 黑米 on 2017/3/27.
 */
@Service
public class BaiJiaHaoCrawler extends BaseCrawler {
    Logger logger = LoggerFactory.getLogger(BaiJiaHaoCrawler.class);

    @Autowired
    private BaiJiaHaoParser baiJiaHaoParser;
    @Autowired
    private ArticleCrawlMapper articleCrawlMapper;

    @Override
    protected String url() {
        return CrawlConstant.BAIJIAHAO_URL;
    }

    @Override
    protected int tag() {
        return CrawlConstant.BAIJIAHAO_TAG;
    }

    @Override
    protected String avatarSelect() {
        return "DIV.author-level-bigger>img";
    }

    @Override
    protected String channelNameSelect() {
        return "h2.title";
    }

    /**
     * 频道名称是否同时是作者名称
     *
     * @return 是、否
     */
    @Override
    protected boolean isAuthorName() {
        return true;
    }

    @Override
    protected String authorLink() {
        return "div.detail>a";
    }

    @Override
    protected String listSelect() {
        return "li.cnt-list";
    }

    @Override
    protected String titleSelect() {
        return "div.text>h2";
    }

    @Override
    protected String authorSelect() {
        return "h2.title";
    }

    @Override
    protected JsonResult crawl(int authorId, String link, Author author, Document doc, boolean all) {
        // 爬取文章
        String id;
        if(link.contains("&wfr")){
            id = link.substring(link.indexOf("app_id=") + 7, link.indexOf("&wfr"));
        }else{
            id = link.substring(link.indexOf("app_id=") + 7,link.length());
        }

        //https://baijiahao.baidu.com/u?app_id=1554781529119683&wfr=null&fr=followcon
        //http://baijiahao.baidu.com/api/content/article/listall?app_id=1554781529119683&_skip=0&_limit=12
        int page = 0;
        int totalAll = 0;
        String json ;
        List<Map<String,Object>> mapList ;
         while (page <= CrawlConstant.CRAWL_PAGE_TOTAL) {
             //http://baijiahao.baidu.com/api/content/article/listall?app_id=1569971631482481&_skip=20&_limit=10&_cache=1
            json = WebUtil.buildRequest("http://baijiahao.baidu.com/api/content/article/listall?app_id="+id+"&_skip=" + page * 20 + "&_limit=10&_cache=1",false);
            Map<String ,Object> map = JsonUtil.getResult(json);
            if(map == null || map.size() == 0){
                break;
            }
             mapList = (List)map.get("items");
            if(ListUtil.isEmpty(mapList)){
                break;
            }

            JsonResult result = iterate(authorId, mapList);
            if (!result.isSuccess()) {
                return result;
            }
            totalAll += result.getInt();
            if (!all && result.getInt() < 1) {
                break;
            }
             page++;
        }
        return totalAll == 0 ? error(CrawlConstant.MSG_WARN_NO_UPDATE) : success(totalAll);
    }

    /**
     * 迭代解析、保存文章
     *
     * @return 成功保存数量
     */
    protected JsonResult iterate(int authorId, List<Map<String,Object>> maps) {

        int succ = 0;
        int exist = 0;
        for (Map<String,Object> map : maps) {
            // 获取标题、链接元素
           String title = (String)map.get("title");
            if (StringUtils.isNotBlank(title)) {
                try {
                    // 校验作者、标题是否已存在
                    Article article = articleCrawlMapper.queryByAuthorTitle(authorId, title);
                    if (article != null) {
                        exist++;
                        if (exist >= 6 && succ == 0) {
                            return success(0);
                        }
                        continue;
                    }
                    String href = (String)map.get("url");
                    JsonResult result = parse(authorId, href);
                    succ += result.isSuccess() ? 1 : 0;
                } catch (Exception e) {
                    logger.error("Parser.parse error,{}", title, e);
                    return error(CommonConstant.ABNORMAL_MSG);
                }
            }
        }
        return success(succ);
    }


    @Override
    protected JsonResult parse(int authorId, String link) throws Exception {
        return baiJiaHaoParser.parse(authorId, link);
    }

    @Override
    protected String getBody(Document doc) {
        return baiJiaHaoParser.getBody(doc);
    }
}
